﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Xml;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Windows.Media;


namespace An_Im
{
    static partial class CoreManipulator
    {
        /// <summary>
        /// 拓展项宿主。
        /// </summary>
        public static Extension.ExtensionHost ExtensionHost { get; private set; }

        /// <summary>
        /// 从指定图形中获取指定效果。
        /// </summary>
        /// <param name="shape">图形对象。</param>
        /// <returns>图形所拥有的动画效果。</returns>
        public static IEnumerable<PPT.Effect> GetEffectsFromShape(PPT.Shape shape)
        {
            return from PPT.Effect eff in GetSlideInGeneralOccassion().TimeLine.MainSequence where eff.Shape == shape select eff;
        }

        /// <summary>
        /// 从指定图形集合中获取指定效果。
        /// </summary>
        /// <param name="shapeRange">图形对象集合。</param>
        /// <returns>图形集合所拥有的动画效果。</returns>
        public static IEnumerable<PPT.Effect> GetEffectsFromShapeRange(PPT.ShapeRange shapeRange)
        {
            if (shapeRange == null) return null;
            var list = new List<PPT.Effect>();
            foreach (PPT.Shape shape in shapeRange)
            {
                list.AddRange(GetEffectsFromShape(shape));
            }
            return list;
        }

        /// <summary>
        /// 从指定图形集合中获取指定效果。
        /// </summary>
        /// <param name="shapeRange">图形对象集合。</param>
        /// <returns>图形集合所拥有的动画效果。</returns>
        public static IEnumerable<PPT.Effect> GetEffectsFromShapeRange()
        {
            return GetEffectsFromShapeRange(GetShapeRange());
        }

        /// <summary>
        /// 通用地获取幻灯片对象的方法。
        /// </summary>
        /// <returns>获得当前幻灯片对象。如果获取不到时返回 null 。</returns>
        public static PPT.Slide GetSlideInGeneralOccassion()
        {
            PPT.Selection sel = null;
            try
            {
                sel = App.ActiveWindow.Selection;
            }
            catch
            {
                return null;
            }

            if (sel.Type == PPT.PpSelectionType.ppSelectionShapes)
            {
                var shapeRange = GetShapeRange();
                if (shapeRange != null)
                    return shapeRange.Parent as PPT.Slide;
                else
                    return null;
            }
            else
            {
                var slideRange = GetSlideRange();
                if (slideRange != null)
                    return slideRange[1];
                else
                    return null;
            }

        }

        /// <summary>
        /// 从选中图形中获取幻灯片对象。
        /// </summary>
        /// <param name="shape">选中的图形对象。</param>
        /// <returns>图形所在的幻灯片对象。如果获取不到时返回 null 。</returns>
        private static PPT.Slide GetSlideFromShape(PPT.Shape shape)
        {
            if (shape == null)
                return null;
            var slide = shape.Parent as PPT.Slide;
            return slide;
        }

        /// <summary>
        /// 获得幻灯片范围。
        /// </summary>
        /// <returns>幻灯片范围。如果获取不到时返回 null 。</returns>
        public static PPT.SlideRange GetSlideRange()
        {
            try
            {
                return App.ActiveWindow.Selection.SlideRange;
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 获得图形范围。
        /// </summary>
        /// <returns>图形范围。如果获取不到时返回 null 。</returns>
        public static PPT.ShapeRange GetShapeRange()
        {
            try
            {
                return App.ActiveWindow.Selection.ShapeRange;
            }
            catch
            {
                return null;
            }
            return null;

        }

        /// <summary>
        /// 是否有存档文件。
        /// </summary>
        public static bool HasSaveFile => File.Exists(SavePath);

        public static void LoadExtensionHost()
        {
            ExtensionHost = Extension.ExtensionLoader.Load();
            foreach (var extension in CoreManipulator.ExtensionHost.Extensions)
            {
                extension.Initialize(App, TaskPaneManager.CustomTaskPaneCollection);

            }

        }


    }
}
